package com.example.radioclub.music.tasks;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.example.radioclub.data.Constants;
import com.example.radioclub.data.Enumerations.RestMethods;
import com.example.radioclub.data.Enumerations.RestSignature;
import com.example.radioclub.music.interfaces.IRestMusic;
import com.example.radioclub.types.RestRequest;
import com.example.radioclub.utilities.FunctionUtils;
import com.example.radioclub.utilities.MelonApiURLsTools;

import android.os.AsyncTask;

/**
 * This is JSON downloader with REST framework and this is general one.
 * 
 * @author wizard
 *
 */
public class RestGeneralJSONDownloader extends AsyncTask<Object, Void, Object>{
	private final String TAG = Constants.APP_TAG + "General Rest Downloader";
	private IRestMusic listener = null;
	private RestTemplate mapiRestTemplate = null;
	
	public RestGeneralJSONDownloader(RestTemplate mapiRestTemplate, IRestMusic listener){
		this.mapiRestTemplate = mapiRestTemplate;
		this.listener = listener;
	}
	
	public void addListener(IRestMusic genListener) {
		this.listener = genListener;
	}
	
	@Override
	protected Object doInBackground(Object... params) {
		// TODO Auto-generated method stub
		if(params.length == 1) {
			if(params[0] instanceof RestRequest) {
				RestRequest request = (RestRequest) params[0];
				if(request.getRequestType() == RestMethods.GET) {
					// do something about GET
					ResponseEntity<String> result = null;
					if(request.getRequestSignature() == RestSignature.GENRE) {
						// do something about GENRE
						result = this.mapiRestTemplate.getForEntity(MelonApiURLsTools.getGenreByAllGenres(), String.class);
					} else if(request.getRequestSignature() == RestSignature.MUSIC) {
						// do something about MUSIC
						result = this.mapiRestTemplate.getForEntity(MelonApiURLsTools.getDailyChartURL(0, 10), String.class);
					} else if(request.getRequestContent() == RestSignature.SEARCH) {
						// do something about SEARCH
						result = this.mapiRestTemplate.getForEntity(MelonApiURLsTools.getSearchIntegrationSong(0, 10, null), String.class);
					}
					return result.getBody();
				} else if (request.getRequestType() == RestMethods.POST) {
					// do something about POST
					if(request.getRequestSignature() == RestSignature.AUTH) {
						// do something about AUTH
						Object o = request.getRequestContent();
					} else if (request.getRequestSignature() == RestSignature.CERTIFICATE) {
						// do something about CERTIFICATE
					}
				} else { }
			}
		}
		return null;
	}
	
}
